package com.zdb.n1.controller;

import com.zdb.n1.entity.FitnessGoal;
import com.zdb.n1.entity.User;
import com.zdb.n1.payload.response.MessageResponse;
import com.zdb.n1.service.FitnessGoalService;
import com.zdb.n1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/fitness-goals")
@PreAuthorize("hasRole('ADMIN')")
public class AdminFitnessGoalController {

    @Autowired
    private FitnessGoalService fitnessGoalService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 获取指定用户的所有健身目标
     * @param userId 用户ID
     * @return 健身目标列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<FitnessGoal>> getUserFitnessGoals(@PathVariable Long userId) {
        User user = userService.getUserById(userId);
        List<FitnessGoal> goals = fitnessGoalService.getGoalsByUser(user);
        return ResponseEntity.ok(goals);
    }
    
    /**
     * 根据状态获取指定用户的健身目标
     * @param userId 用户ID
     * @param status 目标状态
     * @return 健身目标列表
     */
    @GetMapping("/user/{userId}/status/{status}")
    public ResponseEntity<List<FitnessGoal>> getUserFitnessGoalsByStatus(
            @PathVariable Long userId,
            @PathVariable FitnessGoal.GoalStatus status) {
        User user = userService.getUserById(userId);
        List<FitnessGoal> goals = fitnessGoalService.getGoalsByUserAndStatus(user, status);
        return ResponseEntity.ok(goals);
    }
    
    /**
     * 获取指定用户的健身目标详情
     * @param userId 用户ID
     * @param goalId 目标ID
     * @return 健身目标详情
     */
    @GetMapping("/user/{userId}/{goalId}")
    public ResponseEntity<FitnessGoal> getUserFitnessGoalById(
            @PathVariable Long userId,
            @PathVariable Long goalId) {
        
        User user = userService.getUserById(userId);
        FitnessGoal goal = fitnessGoalService.getGoalById(goalId);
        
        // 验证目标属于指定用户
        if (!goal.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健身目标不属于指定用户");
        }
        
        return ResponseEntity.ok(goal);
    }
    
    /**
     * 更新用户的健身目标
     * @param userId 用户ID
     * @param goalId 目标ID
     * @param goal 更新的健身目标
     * @return 更新后的健身目标
     */
    @PutMapping("/user/{userId}/{goalId}")
    public ResponseEntity<FitnessGoal> updateUserFitnessGoal(
            @PathVariable Long userId,
            @PathVariable Long goalId,
            @RequestBody FitnessGoal goal) {
        
        User user = userService.getUserById(userId);
        FitnessGoal existingGoal = fitnessGoalService.getGoalById(goalId);
        
        // 验证目标属于指定用户
        if (!existingGoal.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健身目标不属于指定用户");
        }
        
        // 设置必要的字段
        goal.setId(goalId);
        goal.setUser(user);
        
        // 更新并返回结果
        FitnessGoal updatedGoal = fitnessGoalService.updateGoal(goalId, goal);
        return ResponseEntity.ok(updatedGoal);
    }
    
    /**
     * 更新用户健身目标的进度
     * @param userId 用户ID
     * @param goalId 目标ID
     * @param progress 进度值(0-100)
     * @return 更新后的健身目标
     */
    @PutMapping("/user/{userId}/{goalId}/progress/{progress}")
    public ResponseEntity<FitnessGoal> updateUserGoalProgress(
            @PathVariable Long userId,
            @PathVariable Long goalId,
            @PathVariable Double progress) {
        
        User user = userService.getUserById(userId);
        FitnessGoal existingGoal = fitnessGoalService.getGoalById(goalId);
        
        // 验证目标属于指定用户
        if (!existingGoal.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健身目标不属于指定用户");
        }
        
        // 验证进度值在合理范围内
        if (progress < 0 || progress > 100) {
            throw new RuntimeException("进度值必须在0-100之间");
        }
        
        // 更新进度并返回结果
        FitnessGoal updatedGoal = fitnessGoalService.updateGoalProgress(goalId, progress);
        return ResponseEntity.ok(updatedGoal);
    }
    
    /**
     * 删除用户的健身目标
     * @param userId 用户ID
     * @param goalId 目标ID
     * @return 操作结果消息
     */
    @DeleteMapping("/user/{userId}/{goalId}")
    public ResponseEntity<MessageResponse> deleteUserFitnessGoal(
            @PathVariable Long userId,
            @PathVariable Long goalId) {
        
        User user = userService.getUserById(userId);
        FitnessGoal existingGoal = fitnessGoalService.getGoalById(goalId);
        
        // 验证目标属于指定用户
        if (!existingGoal.getUser().getId().equals(user.getId())) {
            throw new RuntimeException("该健身目标不属于指定用户");
        }
        
        // 删除并返回结果
        fitnessGoalService.deleteGoal(goalId);
        return ResponseEntity.ok(MessageResponse.success("健身目标删除成功"));
    }
} 